<template>
  <div class="mutual">
    <!-- 头部区域 -->
    <div class="tabBar">
        <div v-for="(item, index) in navLists"
        :key="index" class="boxNoClick" :class="{boxClick:changeStyle == index}" @click="isShow(index)">{{item.text}}</div>
    </div>

    <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多数据了"
        @load="onLoad"
      >
      <!-- 待审主要内容区 -->
      <ul v-show="changeStyle == 0" class="leftContent">
        <li v-for="item in list" :key="item.id" @click="jump(item.id)">
          <div class="title">
            <p class="fontSize">{{item.userName}}</p>

            <p class="leftStyle">
              {{item.applyType == 1 ? '年度困难补助申请': (item.applyType == 2 ? '临时困难补助申请':'')}}

              <img src="../imgs/more.png" alt="">
            </p>
          </div>

           <div class="UName" :class="item.applyType == 2 ? 'spanStyleBlue':'spanStyleOrgin'">
              {{item.unionName}}
            </div>

          <div class="times">申请时间：{{item.createTime}}<span class="shenhe" @click.stop="itemData = item,auditJudge()">审核</span></div> 
        </li>
      </ul>

      <!-- 已审主要内容区 -->
      <ul v-show="changeStyle == 1" class="leftContent">
          <li v-for="item in list" :key="item.id" @click="jump(item.id)" >
            <div class="title">
              <p class="fontSize">{{item.userName}}</p>

              <p class="leftStyle">
                {{item.applyType == 1 ? '年度困难补助申请': (item.applyType == 2 ? '临时困难补助申请':'')}}
                
                <img src="../imgs/more.png" alt="">
              </p>
            </div>

            <div class="UName" :class="item.applyType == 2 ? 'spanStyleBlue':'spanStyleOrgin'">
              {{item.unionName}}
            </div>

            <div class="times">申请时间：{{item.createTime}}<span class="shenheTG">{{item.status}}</span></div>
          </li>
      </ul>
    </van-list>

    <van-dialog closeOnClickOverlay v-model="isAudit" title="审核" confirm-button-text="通过" cancel-button-text="不通过" show-cancel-button @confirm="Audit" @cancel="Audit(3)">
      <p style="font-size:14px;text-align:center;padding: 10px 0">是否审核通过</p>
    </van-dialog>
  </div>
</template>

<script>
import Vue from "vue";
import api from '@/data/api/index.js'
import { Dialog } from 'vant'
import { Notify } from 'vant';

// 全局注册
Vue.use(Notify);
Vue.use(Dialog);
export default {
  //过滤器
		filters:{
			formatDate(date) {
				const nDate = new Date(date)
				const y = nDate.getFullYear()
				const m = (nDate.getMonth()+1).toString().padStart(2,0)
				const d = nDate.getDate().toString().padStart(2,0)
				return y+'-'+m+'-'+d
			}
		},
  data() {
    return{
      navLists:[
          {
            "text":"待审"          
          },
          {
            "text":"已审"          
          }
        ],
      changeStyle:0,
      userId: 0,
      list: [],
      count: 0,
      finished: false,
      loading: false,
      params:{
        page:1,
        rows:10
      },
      itemData:{},
      isAudit: false
    }
  },
  mounted () {

  },
  methods: {
    async onLoad(){
      await this.getList()
      this.loading = false;
      // 数据全部加载完成
      if (this.list.length >= this.count) {
        this.finished = true;
      }
    },
    // 审核判断
    auditJudge(){
      if(this.itemData.status == 4){
        this.$troute.query('myHelpDetail', {id:this.itemData.id,status:"audit"})
      } else {
        this.isAudit = true
      }
    },
    // 审核
    Audit(status){
      //itemData.status 0:未审核，1:通过初审，2:终审，3:二审
      let url,data,state;
      data = {
        id: this.itemData.id,
      }
      //3:不通过
        if(this.itemData.status == 0) {
          url = "first"
          data.status=status==3?3:1
        }else if(this.itemData.status == 1){
          url = "second"
          data.status=status==3?3:4
        }

      api.DHAudit(url, {}, data).then(res => {
        if (res.code == "success") {
          Notify({ type: 'success', message: '审批成功' });
        } else {
          Notify({ type: 'danger', message: res.desc });
        }
        this.isAudit = false;
        this.params.page = 1
        this.list = []
        this.getList()
      });
    },
    jump(id){
      this.$troute.query('myHelpDetail', {id:id});
    },
    // tap切换
    isShow(index) {
      this.params.page = 1
      this.list = []
      this.changeStyle = index 
      this.onLoad()
    },
    getList() {
      return new Promise((resolve, reject)=>{
        if(this.changeStyle == 0){
          api.DHGetToAuditList(this.params, { helped: "0"}).then(res => {
            this.list.push(...res.data)
            this.count = res.count
            resolve(res)
          })
          .catch(error => {
            reject(error);
          });
        } else {
          api.DHGetAuditedList(this.params).then(res => {
            res.data.forEach(item=>{
              let statusTxt=['未审核','通过初审','通过终审','未通过','通过二审']
              item.status = statusTxt[item.status]
            })

            this.list.push(...res.data)
            this.count = res.count

            resolve(res)
          })
          .catch(error => {
            reject(error);
          });
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.mutual{
  margin: 0 20px;
  .tabBar{
    width: 100%;
    height: 46px;
    display: flex;
    .boxClick{
      width: 40px;
      height: 100%;
      font-size: 20px;
      font-weight: bold;
      border-bottom: 4px solid rgb(201,21,30);
    }
    .boxNoClick{
      width: 40px;
      height: 100%;
      font-size: 20px;
      margin-left: 20px;
      text-align: center;
    }
  }
  .leftContent li {
    // height: 70px;
    margin-top: 20px;
    padding: 15px 10px 20px;
    background-size: 50px;
    border-radius: 10px;
    box-shadow: 2px 2px 8px #f2f2f2, -2px -2px 8px #f2f2f2;
    .title{
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 16px;
      font-weight: bold;
      .fontSize{
        font-size: 17px;
      }
      .leftStyle{
        img{
          margin-left: 6px;
          width: 8px;
        }
      }
    }
    .UName{
      display: inline-block;
      font-size: 14px;
      font-weight: bold;
      padding: 2px 8px;
      border-radius: 17px;
      margin: 10px 0 0;
    }
    .spanStyleOrgin{
      color: rgb(239,67,20);
      border: 1px solid rgb(239,67,20);
    }
    .spanStyleBlue{
      color: rgb(24,132,204);
      border: 1px solid rgb(24,132,204);
    }
    .times{
      margin-top: 17px;
      font-weight: normal;
      color: rgb(129,129,129);
      font-size: 13px;
      .shenhe{
      width: 60px;
      height: 32px;
      font-weight: bold;
      background-color: rgb(159,16,22);
      border-radius: 18px;
      text-align: center;
      line-height: 32px;
      color: #fff;
      float: right;
      margin-top: -7px;
    }
    .shenheTG{
      font-weight: bold;
      color: rgb(159,16,22);
      float: right;
      font-size: 15px;
    }
    }
  }
}
.router-box{
  padding-top: 54px;
}
</style>